package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.R.attr.format;

public class CryHopeContentActivity extends BaseActivity implements View.OnClickListener{

    private ImageView contentback;
    private ImageView contentuserimg;
    private ImageView contentimg;
    private TextView contenttext;
    private Button contentbt1;
    private Button contentbt2;
    private String imageUrl1;
    private String wish1;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String bonusId;
    private String id;
    private String avatar;
    private String createTime;
    private String imageUrl;
    private String nickname;
    private String range;
    private String time;
    private TextView name;
    private TextView Ttime;
    private TextView where;
    private List<String>list;
    private HashMap<String,String>map;
    private String wish;
    private String type;

    private String format;
    private Date date;
    private Bitmap bitmap;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cry_hope_content);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid","1");
        userId = sharedPreferences.getString("userId", "0");
        token = sharedPreferences.getString("token", "2");
        bonusId = sharedPreferences.getString("bonusId", "3");
        initCryHopeContentView();

        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        list = new ArrayList<>();
        getContentHope();

        //x.image().bind(contentuserimg,InternetAddressUtis.IMG_URL+avatar2);
      //  x.image().bind(contentimg,InternetAddressUtis.IMG_URL+imageUrl2);

    }

    // 获取红包详情
    private void getContentHope() {
        final RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.HOPE_CONTENT);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bonusId",id);
        params.addBodyParameter("params",jsonObject.toJSONString());
       x.http().get(params, new Callback.CommonCallback<String>() {
           @Override
           public void onSuccess(String Object) {
               com.alibaba.fastjson.JSONObject obj = JSON.parseObject(Object);
              String data = obj.getString("data");
               com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
               avatar = obj1.getString("avatar");
               createTime = obj1.getString("createTime");
               wish = obj1.getString("wish");
               imageUrl = obj1.getString("imageUrl");
               nickname = obj1.getString("nickname");
               range = obj1.getString("range");
               type = obj1.getString("type");
               Date date = new Date(Long.valueOf(createTime));
               SimpleDateFormat s = new SimpleDateFormat("MM/dd HH:mm");
               format = s.format(date);
               Picasso.with(CryHopeContentActivity.this)
                       .setIndicatorsEnabled(false);

               Picasso.with(CryHopeContentActivity.this)
                       .load(InternetAddressUtis.IMG_URL+avatar)
                       .transform(new CircleTransform())
                       .noFade()
                       .memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)
                       .centerCrop().resize(200,200)
                       .into(contentuserimg);
/*
                   Picasso.with(CryHopeContentActivity.this)
                           .load(InternetAddressUtis.IMG_URL + imageUrl)
                            .centerCrop().resize(100,100)
                           .into(contentimg);*/
               x.image().bind(contentimg,InternetAddressUtis.IMG_URL+imageUrl);
               name.setText(nickname);
               where.setText(range);
               contenttext.setText(wish);
               Ttime.setText(format);
               SharedPreferences.Editor edit = sharedPreferences.edit();
               edit.putString("avatar",avatar);
               edit.putString("wish",wish);
               edit.putString("imageUrl",imageUrl);
               edit.putString("nickname",nickname);
               edit.putString("range",range);
               edit.putString("format", format);
               edit.commit();


           }

           @Override
           public void onError(Throwable ex, boolean isOnCallback) {
               ErrType.errType(CryHopeContentActivity.this,ex);
           }

           @Override
           public void onCancelled(CancelledException cex) {

           }

           @Override
           public void onFinished() {

           }
       });
    }

    private void initCryHopeContentView() {
        contentback = (ImageView) findViewById(R.id.cryhope_content_back);
        contentuserimg = (ImageView) findViewById(R.id.cryhope_contentuserimg);
        contentimg = (ImageView) findViewById(R.id.cryhope_contentimg);
        contenttext = (TextView) findViewById(R.id.cryhope_contenttext);
        contentbt1 = (Button) findViewById(R.id.cryhope_contentbt1);
        contentbt2 = (Button) findViewById(R.id.cryhope_contentbt2);
        name = (TextView) findViewById(R.id.cryhope_contentname);
        Ttime = (TextView) findViewById(R.id.cryhope_contenttime);
        where = (TextView) findViewById(R.id.cryhope_contentwhere);
        backmain = (ImageView) findViewById(R.id.cryhopecontent_backmain);
        backmain.setOnClickListener(this);
        contentbt2.setOnClickListener(this);
        contentback.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cryhopecontent_backmain:
                startActivity(new Intent(CryHopeContentActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.cryhope_content_back:// 杀死当前页面
                finish();
                break;
            case R.id.cryhope_contentbt1:// 聊天页面
                break;
            case R.id.cryhope_contentbt2:// 跳转到抢红包页面
                Intent in = new Intent(CryHopeContentActivity.this,CryHopeResultActivity.class);
                in.putExtra("id",id);
                in.putExtra("type",type);
                startActivity(in);
                finish();
                break;
        }
    }

}
